//
//  NYSettingCell.m
//  百思不得姐
//
//  Created by Apple on 15/8/27.
//  Copyright (c) 2015年 Neal. All rights reserved.
//

#import "NYSettingCell.h"
#import "NYSettingItem.h"
#import "NYSettingArrowItem.h"
#import "NYSettingSwitchItem.h"
#import "NYSettingSegItem.h"
@implementation NYSettingCell
+ (instancetype)cellWithTableView:(UITableView *)tableView
{
    static NSString *ID = @"settingcell";
    NYSettingCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (cell == nil) {
        cell = [[NYSettingCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
    return cell;
}
- (void)setItem:(NYSettingItem *)item
{
    _item = item;
    [self setdata];
    [self setAccessoryView];
}
- (void)setdata
{
    self.textLabel.text = _item.titlename;
}
- (void)setAccessoryView
{
    if ([_item isKindOfClass:[NYSettingArrowItem class]]) {
        self.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        
    } else if ([_item isKindOfClass:[NYSettingSwitchItem class]]){
        
        NYSettingSwitchItem *item = (NYSettingSwitchItem *)self.item;
        
        UISwitch *sw = [[UISwitch alloc]init];
        sw.on = item.isOpen;
        [sw addTarget:self action:@selector(switchchange:) forControlEvents:UIControlEventValueChanged];
        self.accessoryView = sw;
    }else if ([_item isKindOfClass:[NYSettingSegItem class]])
    {
        NYSettingSegItem *item = (NYSettingSegItem *)_item;
        UISegmentedControl *seg = [[UISegmentedControl alloc]initWithItems:@[@"小",@"中",@"大"]];
        
        if (item.index!= 0 ) {
            seg.selectedSegmentIndex = item.index;
        }else
        {   seg.selectedSegmentIndex = 0;
}
        [seg addTarget:self action:@selector(segmentedControl:) forControlEvents:UIControlEventValueChanged];
        self.accessoryView =seg;
    }else
    {
        self.accessoryView = nil;
    }
}
- (void)switchchange:(UISwitch *)sw
{
    NSLog(@"%d",sw.on);
}
- (void)segmentedControl:(UISegmentedControl *)seg {
    NSLog(@",%zd",seg.selectedSegmentIndex);
}
@end
